﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting.Messaging;

namespace Twitter
{
    public class MessageProcessorContext : IDisposable
    {
        private const string MessageProcessorContextSlotName = "MessageProcessorContextSlotName";
        private List<OutgoingMessage> outgoingMessages = new List<OutgoingMessage>();

        private const int twitterMaxCharCountPerMessage = 140;
        private const string ellipsisString = " ...";

        public MessageProcessorContext(Message incomingMessage)
        {
            if (Current != null) throw new InvalidOperationException("cannot create new MessageProcessorContext, another context is currently active");
            CallContext.SetData(MessageProcessorContextSlotName, this);

            IncomingMessage = incomingMessage;
        }

        public static MessageProcessorContext Current
        {
            get
            {
                return CallContext.GetData(MessageProcessorContextSlotName) as MessageProcessorContext;
            }
        }

        public int TwitterMaxCharCountPerMessage
        {
            get { return twitterMaxCharCountPerMessage; }
        }

        public int SplittedMessageLength
        {
            get {return TwitterMaxCharCountPerMessage - ellipsisString.Length;}
        }

        public void AddOutgoingMessage(string twitterAlias, string message)
        {
            outgoingMessages.Add(new OutgoingMessage { recipient = twitterAlias, message = message });
        }

        public Message IncomingMessage
        {
            get;
            private set;
        }

        public void SendOutgoingMessagesMax140Char(IBotClient client)
        {
            foreach (OutgoingMessage message in outgoingMessages)
            {
                foreach (string messagePart in CalcMessagePartsOf140Chars(message.message))
                {
                    client.Messages.Send(message.recipient, messagePart);
                }
            }
        }

        private IEnumerable<string> CalcMessagePartsOf140Chars(string message)
        {
            int currentMessageStartOffset = 0;

            while (currentMessageStartOffset < message.Length - twitterMaxCharCountPerMessage)
            {
                yield return string.Format("{0}{1}",
                    message.Substring(currentMessageStartOffset, SplittedMessageLength),
                    ellipsisString);

                currentMessageStartOffset += SplittedMessageLength;
            }
            yield return message.Substring(currentMessageStartOffset);
        }

        #region IDisposable Members

        public void Dispose()
        {
            CallContext.SetData(MessageProcessorContextSlotName, null);
        }

        #endregion

        private class OutgoingMessage
        {
            public string recipient;
            public string message;
        }
    }
}
